package com.jsy.common.util.secret;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.Base64;

public class AesEncryptionUtil {
    private static final String ALGORITHM = "AES";
    private static final int KEY_SIZE = 128;

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(encryptedData);
    }

    public static byte[] generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        // 使用 SecureRandom 生成随机密钥
        keyGenerator.init(KEY_SIZE, new SecureRandom());
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    private static void encryptionFile(String filePath) throws Exception {
        // 读取 .ftl 文件
        Path path = Paths.get(filePath);
        byte[] fileContent = Files.readAllBytes(path);

        // 生成密钥
        byte[] key = generateKey();
        String base64Key = Base64.getEncoder().encodeToString(key);
        System.out.println("Generated Key: " + base64Key);

        // 加密文件内容
        byte[] encryptedContent = encrypt(fileContent, key);

        // 将加密后的内容写入新文件
        Path encryptedPath = Paths.get("example_encrypted.ftl");
        Files.write(encryptedPath, encryptedContent);
        System.out.println("File encrypted successfully.");

        // 解密文件内容
        byte[] decryptedContent = decrypt(encryptedContent, key);

        // 将解密后的内容写入新文件
        Path decryptedPath = Paths.get("example_decrypted.ftl");
        Files.write(decryptedPath, decryptedContent);
        System.out.println("File decrypted successfully.");
    }

    public static void main(String[] args) {
        try {
//            encryptionFile("example.ftl");
//            byte[] bytes = generateKey();
//            System.out.println(bytes.length);
//            System.out.println("Generated Key: " + Base64.getEncoder().encodeToString(bytes));
            String passWord = "123456";
            String account = "admin";
            byte[] admins = decrypt("faP79Pc=".getBytes(), new StringBuilder("admin").reverse().toString().getBytes());
            System.out.println(new String(admins));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}    